package com.rf.richfitwheel.admin.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rf.richfitwheel.admin.sys.model.Vacation;
import com.rf.richfitwheel.admin.sys.service.BusTypService;
import com.rf.richfitwheel.admin.sys.service.VacationService;
import com.rf.richfitwheel.common.utils.StringUtils;

import com.rf.richfitwheel.workflow.conf.WorkflowContants;
import com.rf.richfitwheel.workflow.entity.WorkflowMsgVo;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class WorkflowReceiver {

    @Autowired
    private VacationService vacationService;
    @Autowired
    private BusTypService busTypService;

    @RabbitListener(queues = "workflow.system")
    @RabbitHandler
    public void processWorkflowMsg(WorkflowMsgVo workflowMsgVo, Channel channel, @Headers Map<String, Object> headers) throws IOException {
        System.out.println(Thread.currentThread().getName() + " 接收到来自队列的消息：开始========");
        System.out.println(workflowMsgVo.toString());
        System.out.println(Thread.currentThread().getName() + " 接收到来自队列的消息：结束========");
        long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            Map<String, Object> variables = workflowMsgVo.getVariables();
            String processKey = (String) variables.get("processKey");//流程编码
            String approveNo = (String) variables.get("approveNo");//审批编号
            String orgCode = (String) variables.get("orgCode");//企业编码
            String processCode = (String) variables.get("processCode");//流程代码
            String businessKey = (String) variables.get("businessKey");//业务主键
            String processInstanceId = (String) variables.get("processInstanceId");//
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_PROCESS_START)) {//流程开始
                if (StringUtils.isNotBlank(processCode)) {//
                    switch (processCode) {
                        case "10001"://请假流程
                            Vacation vacation = vacationService.getVacationById(businessKey);
                            vacation.setStatus(WorkflowContants.PROCESS_STATUS_1);//更新状态为审批中
                            vacationService.updateVacation(vacation);
                            break;
                        case WorkflowModelCode.FD_OPINION_600109:
                            busTypService.modifySxOpinion(businessKey, WorkflowContants.PROCESS_STATUS_1, workflowMsgVo.getProcessInstanceId());
                            break;
                        default:
                            break;
                    }
                } else {
                    switch (processKey) {//自定义流程
                        case ""://
                        default:
                            break;
                    }
                }

            }
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_PROCESS_END)) {//流程正常结束
                if (StringUtils.isNotBlank(processCode)) {//
                    switch (processCode) {
                        case "10001"://请假流程
                            Vacation vacation = vacationService.getVacationById(businessKey);
                            vacation.setStatus(WorkflowContants.PROCESS_STATUS_2);//更新状态为审批结束
                            vacationService.updateVacation(vacation);
                            break;
                        case WorkflowModelCode.FD_OPINION_600109:
                            busTypService.modifySxOpinion(businessKey, WorkflowContants.PROCESS_STATUS_2, workflowMsgVo.getProcessInstanceId());
                            break;
                        default:
                            break;
                    }
                } else {
                    switch (processKey) {//自定义流程
                        case ""://
                        default:
                            break;
                    }
                }

            }
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_PROCESS_TERMINATED)) {//流程被终止
                if (StringUtils.isNotBlank(processCode)) {//
                    switch (processCode) {
                        case "10001"://请假流程
                            Vacation vacation = vacationService.getVacationById(businessKey);
                            vacation.setStatus(WorkflowContants.PROCESS_STATUS_3);//更新状态为审批终止
                            vacationService.updateVacation(vacation);
                            break;
                        case WorkflowModelCode.FD_OPINION_600109:
                            busTypService.modifySxOpinion(businessKey, WorkflowContants.PROCESS_STATUS_3, workflowMsgVo.getProcessInstanceId());
                            break;
                        default:


                            break;
                    }
                } else {
                    switch (processKey) {//自定义流程
                        case ""://
                        default:
                            break;
                    }
                }
            }
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_TASK_CREATE)) {//当前节点生成审批任务
                String taskState = (String) variables.get("taskState");//参考 WorkflowContants.ProcessResult
                if (StringUtils.isNotBlank(processCode)) {//
                    switch (processCode) {
                        case "10001"://请假流程

                        default:

                            break;
                    }
                } else {
                    switch (processKey) {//自定义流程
                        case ""://
                        default:
                            break;
                    }
                }
            }
            //手工ack
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);
            e.printStackTrace();
        }

    }

}